package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.currenttree;

import java.util.Collection;

import com.gwtext.client.core.Function;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.GWTExtUtil;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.StkosPanelType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.treepanel.StkosCutterClassTreePanel;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.treepanel.StkosCutterIndividualTreePanel;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.cutter.common.treepanel.StkosCutterPropertyTreePanel;
import edu.stanford.bmir.protege.web.client.ui.portlet.AbstractOWLEntityPortlet;

public class StkosCutterCurrentTreePortlet extends AbstractOWLEntityPortlet {

	private StkosCutterClassTreePanel classTreePanel;

	private StkosCutterPropertyTreePanel propertyTreePanel;

	private StkosCutterIndividualTreePanel individualTreePanel;

	private StkosPanelType type;

	private boolean showMapper = false;

	private Project accordingProject;

	public StkosCutterCurrentTreePortlet(Project project) {
		super(project);
	}

	@Override
	public Collection<EntityData> getSelection() {
		return null;
	}

	@Override
	public void onRefresh() {
		if (classTreePanel != null) {
			classTreePanel.refreshTreeNode(getSelectedTreeNode());
		}
		if (propertyTreePanel != null) {
			propertyTreePanel.refreshTreeNode(getSelectedTreeNode());
		}
		if (individualTreePanel != null) {
			individualTreePanel.refreshTreeNode(getSelectedTreeNode());
		}
	}

	@Override
	public void reload() {
	}

	public void setAccordingProject(Project accordingProject) {
		this.accordingProject = accordingProject;
		if (classTreePanel != null) {
			classTreePanel.setAccordingProject(accordingProject);
		}
		if (propertyTreePanel != null) {
			propertyTreePanel.setAccordingProject(accordingProject);
		}
		if (individualTreePanel != null) {
			individualTreePanel.setAccordingProject(accordingProject);
		}
	}

	public boolean isClassTreePanelActivity() {
		boolean f = false;
		if (classTreePanel != null && StkosPanelType.CLASS_TREE.equals(type)) {
			f = true;
		}
		return f;
	}

	public boolean isPropertyTreePanelActivity() {
		boolean f = false;
		if (propertyTreePanel != null
				&& StkosPanelType.PROPERTY_TREE.equals(type)) {
			f = true;
		}
		return f;
	}

	public boolean isIndividualTreePanelActivity() {
		boolean f = false;
		if (individualTreePanel != null
				&& StkosPanelType.INDIVIDUAL_TREE.equals(type)) {
			f = true;
		}
		return f;
	}

	public StkosPanelType getActivityPanelType() {
		return type;
	}

	@Override
	public void initialize() {
		setHeight(500);

		showPanel(StkosPanelType.CLASS_TREE);
	}

	protected void removeAllItems() {
		if (classTreePanel != null) {
			classTreePanel.hide();
		}
		if (propertyTreePanel != null) {
			propertyTreePanel.hide();
		}
		if (individualTreePanel != null) {
			individualTreePanel.hide();
		}
	}

	public void showPanel(StkosPanelType type) {
		removeAll();
		removeAllItems();
		this.type = type;
		if (StkosPanelType.CLASS_TREE.equals(type)) {
			setTitle("当前本体类树");
			if (classTreePanel == null) {
				addClassTreePanel();
			} else {
				classTreePanel.show();
			}
		} else if (StkosPanelType.PROPERTY_TREE.equals(type)) {
			setTitle("当前本体属性树");
			if (propertyTreePanel == null) {
				addPropertyTreePanel();
			} else {
				propertyTreePanel.show();
			}
		} else if (StkosPanelType.INDIVIDUAL_TREE.equals(type)) {
			setTitle("当前本体个体树");
			if (individualTreePanel == null) {
				addIndividualTreePanel();
			} else {
				individualTreePanel.show();
			}
		} else {
			setTitle("当前本体树");
		}
		doLayout();
	}

	protected void addClassTreePanel() {
		classTreePanel = new StkosCutterClassTreePanel(getProject(),
				accordingProject, showMapper);
		if (classTreePanel == null) {
			return;
		}
		classTreePanel.addListener(GWTExtUtil.EVENT_ONSELECTIONCHANGE,
				new Function() {
					@Override
					public void execute() {
						fireOnSelectionChangeEvent();
					}
				});
		add(classTreePanel);
		doLayout();
	}

	protected void addPropertyTreePanel() {
		propertyTreePanel = new StkosCutterPropertyTreePanel(getProject(),
				accordingProject, showMapper);
		if (propertyTreePanel == null) {
			return;
		}
		propertyTreePanel.addListener(GWTExtUtil.EVENT_ONSELECTIONCHANGE,
				new Function() {
					@Override
					public void execute() {
						fireOnSelectionChangeEvent();
					}
				});
		add(propertyTreePanel);
		doLayout();
	}

	protected void addIndividualTreePanel() {
		individualTreePanel = new StkosCutterIndividualTreePanel(getProject(),
				accordingProject, showMapper);
		if (individualTreePanel == null) {
			return;
		}
		individualTreePanel.addListener(GWTExtUtil.EVENT_ONSELECTIONCHANGE,
				new Function() {
					@Override
					public void execute() {
						fireOnSelectionChangeEvent();
					}
				});
		add(individualTreePanel);
		doLayout();
	}

	protected void fireOnSelectionChangeEvent() {
		fireEvent(GWTExtUtil.EVENT_ONSELECTIONCHANGE);
	}

	public TreeNode getSelectedTreeNode() {
		TreeNode treeNode = null;
		if (isClassTreePanelActivity()) {
			treeNode = classTreePanel.getSelectionModel().getSelectedNode();
		}
		if (isPropertyTreePanelActivity()) {
			treeNode = propertyTreePanel.getSelectionModel().getSelectedNode();
		}
		if (isIndividualTreePanelActivity()) {
			treeNode = individualTreePanel.getSelectionModel()
					.getSelectedNode();
		}
		return treeNode;
	}

	public void setShowMapper(boolean showMapper) {
		this.showMapper = showMapper;
		if (classTreePanel != null) {
			classTreePanel.setShowMapper(showMapper);
		}
		if (propertyTreePanel != null) {
			propertyTreePanel.setShowMapper(showMapper);
		}
		if (individualTreePanel != null) {
			individualTreePanel.setShowMapper(showMapper);
		}
	}
}
